﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Xml.Serialization;
using System.IO;
using System.Diagnostics;
using System.Web.Script.Serialization;
namespace SerializeTest
{
    class Program
    {
        static void Main(string[] args)
        {
            var list = new List<Data>();

            Console.WriteLine("data initialize start");

            for (int i = 0; i < 100000; i++)
            {
                list.Add(new Data()
                {
                    DataName = "Data Name " + i,
                    ID = i
                });
            }

            Console.WriteLine("data initialize end");

            XmlSerializer xmlSerilalizer = new XmlSerializer(typeof(List<Data>));
            JsonSerializer jsonSerializer = new JsonSerializer();
           
            JavaScriptSerializer jsonNetSerializer = new JavaScriptSerializer(new SimpleTypeResolver());
            jsonNetSerializer.MaxJsonLength = int.MaxValue;
            string jsonPath = Environment.CurrentDirectory + "\\json.txt";
            string jsonNetPath = Environment.CurrentDirectory + "\\jsonnet.txt";
            string xmlPath = Environment.CurrentDirectory + "\\xml.txt";

            ClearFilePath(jsonPath);
            ClearFilePath(xmlPath);
            ClearFilePath(jsonNetPath);

            var watcher = Stopwatch.StartNew();
            var start = watcher.ElapsedMilliseconds;


            Console.WriteLine("json serialize start :" + start);
            using (StreamWriter writer = new StreamWriter(jsonPath))
            {
                jsonSerializer.Serialize(writer, list);
            }
            var end = watcher.ElapsedMilliseconds;
            Console.WriteLine("json serialize  end:" + end);

            var jsonTimeElapsedResult = (end - start);

            watcher.Reset();


            watcher.Start();
            start = watcher.ElapsedMilliseconds;


            Console.WriteLine("jsonNet serialize start :" + start);
            var stringBuilder = new StringBuilder();
            jsonNetSerializer.Serialize(list, stringBuilder);
            using (StreamWriter writer = new StreamWriter(jsonNetPath))
            {
               
                writer.Write(stringBuilder.ToString());
            }
            end = watcher.ElapsedMilliseconds;
            Console.WriteLine("jsonNet serialize  end:" + end);

            var jsonNetTimeElapsedResult = (end - start);

            watcher.Reset();

            watcher.Start();
            start = watcher.ElapsedMilliseconds;

            Console.WriteLine("xml serialize start :" + start);
            using (StreamWriter writer = new StreamWriter(xmlPath))
            {
                xmlSerilalizer.Serialize(writer, list);
            }
            end = watcher.ElapsedMilliseconds;
            Console.WriteLine("xml serialize  end:" + end);
            var xmlTimeElapsedResult = (end - start);

            Console.WriteLine(jsonTimeElapsedResult - xmlTimeElapsedResult);

            watcher.Reset();


            watcher.Start();
            start = watcher.ElapsedMilliseconds;

            Console.WriteLine("json deserialize start" + start);
            using (StreamReader reader = new StreamReader(jsonPath))
            {
                jsonSerializer.Deserialize(reader, typeof(List<Data>));
            }

            watcher.Stop();
            end = watcher.ElapsedMilliseconds;
            Console.WriteLine("json deserialize end " + end);
            jsonTimeElapsedResult = end - start;

            watcher.Reset();

            watcher.Start();
            start = watcher.ElapsedMilliseconds;

            Console.WriteLine("jsonNet deserialize start" + start);
            string data = string.Empty;
            using (StreamReader reader = new StreamReader(jsonNetPath))
            {
                data = reader.ReadToEnd();  
            }

            jsonNetSerializer.Deserialize(data, typeof(List<Data>));

            watcher.Stop();
            end = watcher.ElapsedMilliseconds;
            Console.WriteLine("jsonNet deserialize end " + end);
            jsonTimeElapsedResult = end - start;

            watcher.Reset();
            watcher.Start();
            start = watcher.ElapsedMilliseconds;

            Console.WriteLine("xml deserialize start" + start);
            using (StreamReader reader = new StreamReader(xmlPath))
            {
                xmlSerilalizer.Deserialize(reader);
            }
            watcher.Stop();
            end = watcher.ElapsedMilliseconds;
            xmlTimeElapsedResult = end - start;
            Console.WriteLine("xml deserialize end " + end);
            Console.WriteLine(jsonTimeElapsedResult - xmlTimeElapsedResult);

            Console.ReadLine();
        }

        static void ClearFilePath(string path)
        {
            File.Delete(path);
            using (File.Create(path))
            {

            }
        }


    }

    public class Data
    {
        public int ID { get; set; }

        public string DataName { get; set; }
    }
}
